package com.cvte.boe.media.controller;

import android.content.Context;
import android.content.Loader;

import com.cvte.boe.media.model.LocalMediaInfo;
import com.cvte.boe.media.utils.constant.Constant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by wuguitong on 2018/4/11.
 */
public class MediaFileManager {
    private static volatile MediaFileManager instance = null;
    private Map<String, List<LocalMediaInfo>> mDirToMediaFileMap;
    private List<LocalMediaInfo> filesList;

    public MediaFileManager() {
        mDirToMediaFileMap = new HashMap<String, List<LocalMediaInfo>>();
    }


    public static MediaFileManager getInstance() {
        if (instance == null){
            synchronized (MediaFileManager.class) {
                if (instance == null) {
                    instance = new MediaFileManager();
                }
            }
        }
        return instance;
    }

    public void removeDirToMediaFilesMap(String dir) {
        mDirToMediaFileMap.remove(dir);
    }

    public void removePathDirToMediaFilesMap(String path) {
        Iterator<Map.Entry<String,List<LocalMediaInfo>>> it  = mDirToMediaFileMap.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String,List<LocalMediaInfo>> entry = it.next();
            if(entry.getKey().startsWith(path)){
                it.remove();
            }
        }
    }

    public void putDirToMediaFilesMap(String dir, List<LocalMediaInfo> list) {
        if(!mDirToMediaFileMap.containsKey(dir)) {
            List<LocalMediaInfo> tmplist = new ArrayList<LocalMediaInfo>();
            tmplist.addAll(list);
            mDirToMediaFileMap.put(dir, tmplist);
        }
    }

    public boolean isContainDir(String dir) {
        return mDirToMediaFileMap.containsKey(dir);
    }

    public void cleanToDirMediaFilesMap() {
        mDirToMediaFileMap.clear();
    }

    public List<LocalMediaInfo> getDirToMediaFilesMap(String dir) {
        List<LocalMediaInfo> list = new ArrayList<LocalMediaInfo>();
        list.addAll(mDirToMediaFileMap.get(dir));
        return list;
    }

    public List<LocalMediaInfo> getMediaFilesByType(String dir, int type) {
        filesList = new ArrayList<LocalMediaInfo>();
        filesList.clear();
        if (mDirToMediaFileMap.containsKey(dir)) {
            List<LocalMediaInfo> listInfos = mDirToMediaFileMap.get(dir);
            for (LocalMediaInfo info : listInfos) {
                if (info.getFileType() == type) {
                    filesList.add(info);
                }
            }
        }
        return filesList;
    }
}
